package com.aizuda.encrypt.helper;

import cn.hutool.crypto.symmetric.AES;
import cn.hutool.crypto.symmetric.SM4;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import cn.hutool.extra.spring.SpringUtil;
import com.aizuda.encrypt.enums.EncryptType;
import com.aizuda.encrypt.service.TransTableService;

import java.nio.charset.StandardCharsets;

/**
 * 加密助手
 *
 * @author nn200433
 * @date 2024-03-26 09:37:26
 */
public class CryptoHelper {

    /**
     * 加解密处理程序
     *
     * @param type 加解密类型
     * @param key  密钥
     * @return {@link SymmetricCrypto }
     * @author nn200433
     */
    public static SymmetricCrypto cryptoHandler(EncryptType type, String key) {
        SymmetricCrypto handler  = null;
        final byte[]    keyBytes = key.getBytes(StandardCharsets.UTF_8);
        switch (type) {
            case AES:
                handler = new AES(keyBytes);
                break;
            case SM4:
            default:
                handler = new SM4(keyBytes);
        }
        return handler;
    }

    /**
     * 获取翻译密码本
     *
     * @return {@link TransTableService }
     * @author nn200433
     */
    public static TransTableService transTable() {
        return SpringUtil.getBean(TransTableService.class);
    }

}
